import java.util.*;

/**
 * This program checks that an entered string
 * is an anagram of an another entered string
 *
 * @author Sergii Koziumynskyi
 * @version 0.01 2014-01-16
 */

public class Anagram {
    public static void main(String[] args) {
        // preparations
        // ask user about values
        Scanner in = new Scanner(System.in);

        System.out.println("Enter a first string using Latin characters.");
        String line1tmp = in.nextLine().trim().toLowerCase();

        System.out.println("Enter a second string using Latin characters.");
        String line2tmp = in.nextLine().trim().toLowerCase();

        // convert first line to an ArrayList (removing spaces)
        ArrayList<String> line1 = new ArrayList<String>();
        for (int i = 0; i < line1tmp.length(); i++) {
            String z = line1tmp.substring(i, ++i);
            i--;
            if (!z.equals(" ")) line1.add(z);
        }

        // convert second line to an ArrayList (removing spaces)
        ArrayList<String> line2 = new ArrayList<String>();
        for (int i = 0; i < line2tmp.length(); i++) {
            String z = line2tmp.substring(i, ++i);
            i--;
            if (!z.equals(" ")) line2.add(z);
        }

        // main calculations
        boolean missedLetter = false;
        notAnnagram:
        for (int i = 0; i < line1.size(); i++) {
            letterFound:
            for (int j = 0; j < line2.size(); j++) {
                if (line1.get(i).equals(line2.get(j))) // letter found; stop search of current letter and seek next
                {
                    line2.remove(j);
                    break letterFound;
                } else if (j == line2.size() - 1) // a searched letter is missed; strings are not anagrams; stop search;
                {
                    missedLetter = true;
                    break notAnnagram;
                }
            }
        }

        // print results
        if (missedLetter) {
            System.out.println("Entered strings are NOT anagrams");
        } else if (line2.size() > 0) {
            System.out.println("Entered strings are anagrams " +
                    "but following letter(s) from second string are redundant: " + line2);
        } else {
            System.out.println("Entered strings are anagrams");
        }
    }
}